package com.afterRain.admin.controller;

import java.io.IOException;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.afterRain.commonly.Prompt;
import com.afterRain.model.CrawlerSimple;
import com.afterRain.util.HttpParsingUtil;
import com.afterRain.util.OkHttpUtil;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.ext.interceptor.POST;

import okhttp3.Response;

/**
* @author 作者 E-mail:
* @version 创建时间：2017年5月10日 下午3:17:26
* 类说明
*/
public class CrawlerController extends Controller{

	
	public void index(){
		render("simple.ftl");
	}
	
	@Before(POST.class)
	public void create_crawler(){
		CrawlerSimple crawlerSimple = getModel(CrawlerSimple.class, "");
		crawlerSimple.setCreateDate(new Date());
		Response response = OkHttpUtil.requestUrl(crawlerSimple.getUrl());
		try {
			LinkedHashSet<String> hashSet =HttpParsingUtil.parsingUrl(response.body().string());
			for (String string : hashSet) {
				System.out.println(string);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		/*if(crawlerSimple.save()){
			renderJson(Prompt.saveerror);
			return;
		}
		renderJson(Prompt.success);*/
		renderHtml("");
	}
}
